<?php
/**
 * User: zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/12/20
 * Time: 17:08
 */

namespace App\Http\Controllers\V1;

use App\Http\Controllers\Controller;
use App\Providers\MyJwt;
use App\Services\DepartmentService;
use App\Services\ManagerService;
use App\Services\Service;
use App\Services\UserService;
use Illuminate\Http\Request;

class ManagerController extends Controller
{

    private $jwtCode;

    public function __construct(Request $request)
    {
        parent::__construct($request);
        $authorization = $request->header('Authorization');
        $jwt = new MyJwt();
        $jwtCode = $jwt->decode($authorization, MyJwt::ENCODE_KEY);
        if (!$jwtCode || !array_key_exists('uid', $jwtCode)) {
            $this->sendJson([], 4031, '请授权');
        }
        $this->jwtCode = $jwtCode;
    }

    /**
     * 党员列表
     */
    public function users()
    {
        $this->sendGet(function() {
            $search = $this->request->get('search', '');
            $role = $this->request->get('role', 0);
            $page = $this->request->get('page', 1);
            $pageSize = $this->request->get('page_size', 10);
            $offset = ($page - 1) * $pageSize;
            $data = ManagerService::getUsers($pageSize, $offset, $role, $search);
            $this->sendJson($data);
        });
    }

    public function userDetail()
    {
        $this->sendGet(function() {
            $userId = $this->request->get('user_id');
            $data = ManagerService::getUserDetail($userId);
            $this->sendJson($data);
        });
    }

    public function deleteUser()
    {
        $this->sendPost(function() {
            $userId = $this->request->get('id');
            ManagerService::deleteUser($userId);
            $this->sendJson([]);
        });
    }

    /**
     * 支部列表
     */
    public function departments()
    {
        $this->sendGet(function() {
            $search = $this->request->get('search', '');
            $page = $this->request->get('page', 1);
            $pageSize = $this->request->get('page_size', 10);
            $offset = ($page - 1) * $pageSize;
            $data = ManagerService::getDepartments($pageSize, $offset, $search);
            $this->sendJson($data);
        });
    }

    public function departmentDetail()
    {
        $this->sendGet(function() {
            $departmentId = $this->request->get('department_id', 0);
            $data = ManagerService::getDepartmentDetail($departmentId);
            $this->sendJson($data);
        });
    }

    public function deleteDepartment()
    {
        $this->sendPost(function() {
            $departmentId = $this->request->get('id');
            ManagerService::deleteDepartment($departmentId);
            $this->sendJson([]);
        });
    }

    /**
     * 打分
     */
    public function saveDepartmentIncrease()
    {
        $this->sendPost(function() {
            $params = $this->request->get('params');
            ManagerService::saveDepartmentIncrease($params['id'], $params['score']);
            $this->sendJson([]);
        });
    }

    public function departmentFile()
    {
        $this->sendGet(function() {
            $departmentId = $this->request->get('id');
            $data = DepartmentService::getDetail($departmentId);
            $this->sendJson($data);
        });
    }

    public function rules()
    {
        $this->sendGet(function() {
            $search = $this->request->get('search', '');
            $role = $this->request->get('role', 0);
            $limit = $this->request->get('limit', 10);
            $offset = $this->request->get('offset', 0);
            $data = ManagerService::getManagers($limit, $offset, $role, $search);
            $this->sendDataTable($data);
        });
    }

    public function saveManager()
    {
        $this->sendPost(function() {
            $params = $this->request->get('params');
            if (array_key_exists('password', $params)) {
                $params['password'] = md5($params['password']);
            }
            ManagerService::saveManager($params);
            $this->sendJson([]);
        });
    }

    public function simpleDepartments()
    {
        $this->sendGet(function() {
            $data = ManagerService::getSimpleDepartments();
            $this->sendJson($data);
        });
    }

    public function ruleDetail()
    {
        $this->sendGet(function() {
            $id = $this->request->get('id');
            $data = ManagerService::getManagerDetail($id);
            $this->sendJson($data);
        });
    }

    public function deleteManager()
    {
        $this->sendPost(function() {
            $id = $this->request->get('id');
            ManagerService::deleteManager($id);
            $this->sendJson([]);
        });
    }

    /**
     * 评分文件列表
     */
    public function contest()
    {
        $this->sendGet(function() {
            $type = $this->request->get('type');
            $limit = $this->request->get('limit', 10);
            $offset = $this->request->get('offset', 0);
            $data = Service::getContest($limit, $offset, $type);

            $this->sendDataTable($data);
        });
    }

    /**
     * 评分文件详情
     */
    public function contestDetail()
    {
        $this->sendGet(function() {
            $id = $this->request->get('id');
            $data = Service::getContestDetail($id);
            $this->sendJson($data);
        });
    }

    public function saveContest()
    {
        $this->sendPost(function() {
            $params = $this->request->get('params');
            ManagerService::saveContest($params);
            $this->sendJson([]);
        });
    }

    public function deleteContest()
    {
        $this->sendPost(function() {
            $id = $this->request->get('id');
            ManagerService::deleteContest($id);
            $this->sendJson([]);
        });
    }

    public function growRules()
    {
        $this->sendGet(function() {
            $type = $this->request->get('type');
            $limit = $this->request->get('limit', 10);
            $offset = $this->request->get('offset', 0);
            $data = ManagerService::getGrowRules($limit, $offset, $type);
            $this->sendDataTable($data);
        });
    }

    public function growRuleDetail()
    {
        $this->sendGet(function() {
            $id = $this->request->get('id');
            $data = ManagerService::getGrowRuleDetail($id);
            $this->sendJson($data);
        });
    }

    public function saveGrowRule()
    {
        $this->sendPost(function() {
            $params = $this->request->get('params');
            $params['manager_id'] = $this->jwtCode['uid'];
            ManagerService::saveGrowRule($params);
            $this->sendJson([]);
        });
    }

    public function deleteGrowRule()
    {
        $this->sendPost(function() {
            $id = $this->request->get('id');
            ManagerService::deleteGrowRule($id);
            $this->sendJson([]);
        });
    }

    /**
     * 建议
     */
    public function adviceToUser()
    {
        $this->sendGet(function() {
            $uid = $this->request->get('user_id');
            $year = $this->request->get('year', 0);
            $page = $this->request->get('page', 1);
            $pageSize = $this->request->get('page_size', 10);
            $offset = ($page - 1) * $pageSize;
            $data = ManagerService::adviceToUser($uid, $pageSize, $offset, $year);
            $this->sendDataTable($data);
        });
    }

    public function adviceToDepartment()
    {
        $this->sendGet(function() {
            $departmentId = $this->request->get('department_id');
            $year = $this->request->get('year', 0);
            $page = $this->request->get('page', 1);
            $pageSize = $this->request->get('page_size', 10);
            $offset = ($page - 1) * $pageSize;
            $data = ManagerService::adviceToDepartment($departmentId, $pageSize, $offset, $year);
            $this->sendDataTable($data);
        });
    }



    public function departmentScoreFile()
    {
        $this->sendGet(function(){
            $departmentId = $this->request->get('department_id');
            $data = ManagerService::getDepartmentScoreFile($departmentId);
            $this->sendJson($data);
        });
    }

    public function departmentScoreFileDetail()
    {
        $this->sendGet(function(){
            $id = $this->request->get('id');
            $departmentId = $this->request->get('department_id');
            $data = ManagerService::getDepartmentScoreFileDetail($id, $departmentId);
            $this->sendJson($data);
        });
    }

    /**
     * 加分列表
     */
    public function increaseScore()
    {
        $this->sendGet(function() {
            $search = $this->request->get('search', '');
            $role = $this->request->get('role');
            $limit = $this->request->get('limit', 10);
            $offset = $this->request->get('offset', 0);
            $data = ManagerService::getIncreaseScore($limit, $offset, $role, $search);
            $this->sendDataTable($data);
        });
    }

    /**
     * 加分详情
     */
    public function increaseScoreDetail()
    {
        $this->sendGet(function() {
            $id = $this->request->get('id');
            $data = Service::getIncreaseScoreDetail($id);
            $this->sendJson($data);
        });
    }

    public function updateUserIncreaseScore()
    {
        $this->sendPost(function (){
            $params = $this->request->get('params');
            ManagerService::updateUserIncreaseScore($params);
            $this->sendJson([]);
        });
    }

    public function updateDepartmentIncreaseScore()
    {
        $this->sendPost(function (){
            $params = $this->request->get('params');
            ManagerService::updateDepartmentIncreaseScore($params);
            $this->sendJson([]);
        });
    }

    public function deleteIncreaseScore()
    {
        $this->sendPost(function (){
            $id = $this->request->get('id');
            ManagerService::deleteIncreaseScore($id);
            $this->sendJson([]);
        });
    }

    public function saveUserScoreFileScore()
    {
        $this->sendPost(function(){
            $params = $this->request->get('params');
            $id = $params['id'];
            $userId = $params['uid'];
            $score = $params['score'];
            ManagerService::saveUserScoreFileScore($id, $userId, $score);
            $this->sendJson([]);
        });
    }

    public function saveDepartmentScoreFileScore()
    {
        $this->sendPost(function(){
            $params = $this->request->get('params');
            $id = $params['id'];
            $departmentId = $params['department_id'];
            $score = $params['score'];
            ManagerService::saveDepartmentScoreFileScore($id, $departmentId, $score);
            $this->sendJson([]);
        });
    }

    public function saveAdviceToUser()
    {
        $this->sendPost(function(){
            $params = $this->request->get('params');
            ManagerService::saveAdvice($params['content'], $params['user_id']);
            $this->sendJson([]);
        });
    }

    public function saveAdviceToDepartment()
    {
        $this->sendPost(function(){
            $params = $this->request->get('params');
            ManagerService::saveAdvice($params['content'], 0, $params['department_id']);
            $this->sendJson([]);
        });
    }

    public function scoreFiles()
    {
        $this->sendGet(function (){
            $type = $this->request->get('type', '');
            $year = $this->request->get('year');
            $role = $this->request->get('role', 0);
            $limit = $this->request->get('limit', 10);
            $offset = $this->request->get('offset', 0);
            $data = ManagerService::scoreFiles($limit, $offset, $type, $year, $role);
            $this->sendDataTable($data);
        });
    }

    public function scoreFileDetail()
    {
        $this->sendGet(function (){
            $id = $this->request->get('id', 0);
            $data = ManagerService::scoreFileDetail($id);
            $this->sendJson($data);
        });
    }

    public function saveScoreFile()
    {
        $this->sendPost(function (){
            $params = $this->request->get('params');
            $params['manager_id'] = $this->jwtCode['uid'];
            ManagerService::saveScoreFile($params);
            $this->sendJson([]);
        });
    }

    public function deleteScoreFile()
    {
        $this->sendPost(function (){
            $id = $this->request->get('id', 0);
            ManagerService::deleteScoreFile($id);
            $this->sendJson([]);
        });
    }

    public function dataUsers()
    {
        $this->sendGet(function (){
            $data = ManagerService::getDataUsers();
            $this->sendJson($data);
        });
    }

    public function userScoreFiles()
    {
        $this->sendGet(function (){
            $uid = $this->request->get('uid');
            $data = ManagerService::getUserScoreFile($uid);
            $this->sendDataTable($data);
        });
    }

    public function userScoreFileDetail()
    {
        $this->sendGet(function(){
            $id = $this->request->get('id');
            $uid = $this->request->get('user_id');
            $data = ManagerService::getUserScoreFileDetail($id, $uid);
            $this->sendJson($data);
        });
    }

    public function departmentScoreFiles()
    {
        $this->sendGet(function (){
            $departmentId = $this->request->get('department_id');
            $data = ManagerService::getDepartmentScoreFile($departmentId);
            $this->sendDataTable($data);
        });
    }

    public function userIncreaseScore()
    {
        $this->sendGet(function (){
            $limit = $this->request->get('limit', 10);
            $offset = $this->request->get('offset', 0);
            $uid = $this->request->get('uid');
            $data = UserService::getIncreaseScore($uid, $limit, $offset);
            $this->sendDataTable($data);
        });
    }

    public function departmentIncreaseScore()
    {
        $this->sendGet(function (){
            $limit = $this->request->get('limit', 10);
            $offset = $this->request->get('offset', 0);
            $departmentId = $this->request->get('department_id');
            $data = DepartmentService::getIncreaseScore($departmentId, $limit, $offset);
            $this->sendDataTable($data);
        });
    }

    public function departmentUsers()
    {
        $this->sendGet(function (){
            $departmentId = $this->request->get('department_id');
            $data = DepartmentService::getSimpleUsers($departmentId);
            $this->sendJson($data);
        });
    }

    public function checkUsername()
    {
        $this->sendGet(function (){
            $username = $this->request->get('username');
            $data = ManagerService::checkUsername($username);
            $this->sendJson($data);
        });
    }

    public function userLevel()
    {
        $this->sendGet(function() {
            $uid = $this->request->get('user_id');
            $data = ManagerService::getUserLevel($uid);
            $this->sendJson($data);
        });
    }

    public function saveUserLevel()
    {
        $this->sendPost(function(){
            $params = $this->request->get('params');
            ManagerService::saveUserLevel($params);
            $this->sendJson([]);
        });
    }
}